package cn.kgc.crowdfundingadminspringboot.controller;

import cn.kgc.crowdfundingadminspringboot.common.CrowdFundingConstant;
import cn.kgc.crowdfundingadminspringboot.common.ResultEntity;
import cn.kgc.crowdfundingadminspringboot.pojo.Cert;
import cn.kgc.crowdfundingadminspringboot.service.CertService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/cert")
public class CertController {
    @Resource
    CertService certService;
    @RequestMapping("/to/page.html")
    public String toPage(){
        return "cert-page";
    }

    /**
     * 查询资质
     * @param pageNum
     * @param pageSize
     * @param keyword
     * @param model
     * @return
     */
    @RequestMapping("/query/for/search")
    public String queryForSearch(
            // 如果页面上没有提供对应的请求参数，那么可以使用defaultValue指定默认值
            @RequestParam(value="pageNum", defaultValue="1") Integer pageNum,
            @RequestParam(value="pageSize", defaultValue="5") Integer pageSize,
            @RequestParam(value="keyword", defaultValue="") String keyword,
            Model model) {
        PageInfo<Cert> pageInfo = certService.queryForKeywordSearch(pageNum, pageSize, keyword);
        System.out.println(pageInfo.getList());
        System.out.println(pageInfo.getPageSize());
        System.out.println(pageInfo.getPageNum());
        model.addAttribute(CrowdFundingConstant.ATTR_NAME_PAGE_INFO, pageInfo);
        return "cert-page";
    }

    @ResponseBody
    @RequestMapping("/search/by/keyword")
    public ResultEntity<PageInfo<Cert>> search(
            @RequestParam(value="pageNum", defaultValue="1",required = false) Integer pageNum,
            @RequestParam(value="pageSize", defaultValue="5",required = false) Integer pageSize,
            @RequestParam(value="keyword", defaultValue="",required = false) String keyword
    ) {
        // 1.查询得到PageInfo对象
        PageInfo<Cert> pageInfo = certService.queryForKeywordSearch(pageNum, pageSize, keyword);
        // 2.封装结果对象返回
        return ResultEntity.successWithoutData(pageInfo);
    }


    @ResponseBody
    @RequestMapping("/get/list/by/id/list")
    public ResultEntity<List<Cert>> getRoleListByIdList(@RequestBody List<Integer> roleIdList) {
        List<Cert> roleList = certService.getCertListByIdList(roleIdList);
        return ResultEntity.successWithoutData(roleList);
    }

    @ResponseBody
    @RequestMapping("/batch/remove")
    public ResultEntity<String> batchRemove(@RequestBody List<Integer> roleIdList) {
        certService.batchRemove(roleIdList);
        return ResultEntity.successWithoutData();
    }


    @ResponseBody
    @RequestMapping("/save/cert")
    public ResultEntity<String> saveRole(@RequestParam("name") String name) {

        certService.saveCert(name);

        return ResultEntity.successWithoutData();
    }


    @ResponseBody
    @RequestMapping("/update/cert")
    public ResultEntity<String> updateCert(Cert cert) {
        certService.updateCert(cert);
        return ResultEntity.successWithoutData();
    }
}
